{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The ``inline`` flag will use the appropriate backend to make figures appear inline in the notebook.  \n",
    "%matplotlib inline\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# `plt` is an alias for the `matplotlib.pyplot` module\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# import seaborn library (wrapper of matplotlib)\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load Data\n",
    "To be able to graph data, you need to have data. We will load a dataset that is a payment table of the 34690 dollar car loan at 7.02% over 60 months.\n",
    "\n",
    "Data originally taken from [here](https://towardsdatascience.com/the-cost-of-financing-a-new-car-car-loans-c00997f1aee)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load car loan data into a pandas dataframe from a csv file\n",
    "filename = 'data/table_i702t60.csv'\n",
    "df = pd.read_csv(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# View the first 5 rows of the dataframe\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Checking to make sure we dont have nans in our dataframe\n",
    "# You can't directly plot data that contains nans\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# For this notebook we will graph interest_paid, principal_paid, and month on one graph\n",
    "# While we could graph directly through pandas, we will graph through matplotlib for this video\n",
    "month_number = df.loc[:, 'month'].values\n",
    "interest_paid = df.loc[:, 'interest_paid'].values\n",
    "principal_paid = df.loc[:, 'principal_paid'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "month_number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The values attribute converts a column of values into a numpy array\n",
    "type(month_number)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Keep in mind that we havent gotten to every parameter of `plt.plot`. If you want to learn more, you can see the [documentation](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.plot.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MATLAB-style vs Object Syntax\n",
    "Matplotlib has two different types of syntax.\n",
    "\n",
    "<b>MATLAB-style</b>\n",
    "\n",
    "This is a scripted interface designed to feel like MATLAB. Matplotlib maintains a pointer to the current (active) figure and sends commands to it. \n",
    "\n",
    "<b>Object-oriented</b>\n",
    "\n",
    "This is more often used in situations where you want more control over your figure. \n",
    "\n",
    "<b>Important Note</b>\n",
    "You can and often will have plots that will be created through a combination of MATLAB-style and object-oriented syntax. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MATLAB-style"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.style.use('seaborn')\n",
    "\n",
    "plt.plot(month_number, interest_paid, c= 'k')\n",
    "plt.plot(month_number, principal_paid, c = 'b')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Object-oriented"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# tuple unpacking\n",
    "x, y = (3, 9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(nrows = 1, ncols = 1)\n",
    "axes.plot(month_number, interest_paid, c= 'k');\n",
    "axes.plot(month_number, principal_paid, c = 'b');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Combination"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(nrows = 1, ncols = 1)\n",
    "plt.plot(month_number, interest_paid, c= 'k')\n",
    "axes.plot(month_number, principal_paid, c = 'b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
